<?php
// 应用公共文件
//自定义资金明细整理
/***  资金明细
 * @param $userId           用户id
 * @param $amount_money     操作金额
 * @param $type             操作类型
 * @param $balance          当前可用余额
 * @param $toId             关联id
 * @param string $aId       活动编号
 * @param string $remarks   特殊备注
 * @return array
 */
function detailed($userId,$amount_money,$type,$balance,$toId,$aId = '',$remarks = '',$no_order = ''){
    switch ($type){
        case 'recharge': //连连充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'recharge',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '连连充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'recharge_hzpay': //杭州银行自助充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'recharge_hzpay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '自助充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'arrears': //欠款扣款
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'arrears',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '扣款-抵扣负债'.$amount_money.'元',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'recharge_wxpay': //微信充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'recharge_wxpay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '微信充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'recharge_alipay': //支付宝充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'recharge_alipay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '支付宝充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'adapay_alipay': //汇付天下支付宝充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'adapay_alipay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '支付宝充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'adapay_wxpay': //汇付天下微信充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'adapay_wxpay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '微信充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'phone_wxpay': //商家手机端微信充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'phone_wxpay',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '公众号微信充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'recharge_admin': //后台充值
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'recharge_admin',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '后台充值',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'rebate_admin': //后台返利
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'rebate_admin',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '后台返利',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'after_sales_admin': //后台售后
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'after_sales_admin',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '后台售后',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'deduct_admin': //后台扣款
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'deduct_admin',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '后台扣款',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'deduct_admin_arrears': //后台扣款负债   后台扣款--余额不足 -- 负债
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'deduct_admin_arrears',
                'balance' => 0,
                'remarks' => '后台扣款,余额不足,欠款',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'withdrawal_freeze': //提现冻结
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 0,
                'type' => 'withdrawal_freeze',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '提现冻结('.$remarks.')',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'withdrawal_success': //提现成功
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'withdrawal_success',
                'balance' => $balance,
                'remarks' => '提现成功('.$remarks.')',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'withdrawal_fee': //提现成功 -- 扣除手续费
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'withdrawal_fee',
                'balance' => $balance,
                'remarks' => '提现成功('.$remarks.'),手续费扣除',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'withdrawal_unfreeze': //提现失败 解冻
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 0,
                'type' => 'withdrawal_unfreeze',
                'balance' => bcadd($balance,$amount_money,2),
                'remarks' => '提现失败('.$remarks.')',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'shop_pay': //购买店铺支出
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 2,
                'type' => 'shop_pay',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '购买店铺支出',
                'to_id' => $toId,
                'addtime' => time(),
            ];
            break;
        case 'activity_commission_freeze': //活动发布冻结 -- 冻结佣金
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 0,
                'type' => 'activity_commission_freeze',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '活动发布冻结佣金',
                'to_id' => $toId,
                'a_id' => $aId,
                'addtime' => time(),
            ];
            break;
        case 'order_wxpay': //样品订单-微信支付
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'order_wxpay',
                'balance' => $balance,
                'remarks' => '订单支付-微信支付',
                'to_id' => $toId,
                'a_id' => $aId,
                'addtime' => time(),
                'no_order' => $no_order,
            ];
            break;
        case 'order_balance_wxpay': //订单支付-余额支付
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 1,
                'type' => 'sample_order_balance_wxpay',
                'balance' => bcsub($balance,$amount_money,2),
                'remarks' => '订单支付-余额支付',
                'to_id' => $toId,
                'a_id' => $aId,
                'addtime' => time(),
            ];
            break;
        default:
            $detailed = [
                'uid' => $userId,
                'amount_money' => $amount_money,
                'state' => 0,
                'type' => $type,
                'balance' => $balance,
                'remarks' => '未知类型',
                'to_id' => $toId,
                'a_id' => $aId,
                'addtime' => time(),
            ];
    }

    return $detailed;
}


//对象转数组 使用中
function object_array($array){
    if(is_object($array)){
        $array = (array)$array;
    }
    if(is_array($array)){
        foreach($array as $key=>$value){
            $array[$key] = object_array($value);
        }
    }
    return $array;
}


function str_random($length = 16)
{
    $string = '';
    while (($len = strlen($string)) < $length) {
        $size = $length - $len;
        $bytes = random_bytes($size);
        $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
    }
    return $string;
}

